package cn.edu.imnu.Controller;
import cn.edu.imnu.Service.AccountService;
import cn.edu.imnu.Service.StudentService;
import cn.edu.imnu.entity.JwtUtil;
import cn.edu.imnu.entity.Result;
import cn.edu.imnu.entity.dto.AccountLogindto;
import cn.edu.imnu.entity.dto.StudentLogindto;
import cn.edu.imnu.entity.po.Student;
import cn.edu.imnu.entity.vo.AccountLoginvo;
import cn.edu.imnu.entity.vo.StudentLoginvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@RestController
@ResponseBody
@RequestMapping("/account")
public class StudentController {
    @Autowired
    private AccountService accountService;
//    private StudentService studentService;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody AccountLogindto accountLogindto){
        AccountLoginvo accountLoginvo = accountService.login(accountLogindto);
        if(Objects.isNull(accountLoginvo)){
            return Result.error("用户名或者密码错误");
        }else{
            Map<String , Object> claims = new HashMap<>();
            claims.put("id",accountLoginvo.getId());
            claims.put("uname",accountLoginvo.getUname());
            claims.put("realname",accountLoginvo.getRealname());
            claims.put("pwd",accountLoginvo.getPwd());
            String token  = JwtUtil.genToken(claims);
            Map<String,Object> result=new HashMap<>();
           result.put("token",token);
           result.put("userInfo",accountLoginvo);
//          ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
//            operations.set(token,token,1, TimeUnit.HOURS);
            return Result.success(result);
        }
    }
}
